package com.example.demo.controller;

import com.example.demo.bean.User;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
public class HelloSender2 {
    @Autowired
    private RabbitTemplate template;

    @Autowired
    MessagePostProcessor correlationIdProcessor;

    /**
     * 一个生产者，一个消费者
     */
    @GetMapping("hello")
    public void send() {
        String context = "hello " + new Date();
        System.out.println("Sender:" + context);
        //开启生产者confirm模式
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        template.convertAndSend("hello", (Object) context, correlationIdProcessor, correlationData);
    }


    @GetMapping("helloObject")
    public void sendObject() {
        User user = new User("张三", 16);
        System.out.println("Sender:" + user);
        //开启生产者confirm模式
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        template.convertAndSend("helloObject", user, correlationIdProcessor, correlationData);
    }


    /**
     * 一个消息产生者，多个消息的消费者。竞争抢消息 消息会均匀分配到多个消费者
     */
    @GetMapping("hello2")
    public void send2() {
        CorrelationData correlationData = new CorrelationData();
        String context = "spring boot neo queue";
        System.out.println("Sender1:" + context);
        for (int i = 0; i < 10; i++) {
            correlationData.setId(UUID.randomUUID().toString());
            template.convertAndSend("neo", (Object) (context + i), correlationIdProcessor, correlationData);
        }
    }

    /**
     * 生产者将消息不是直接发送到队列，而是发送到X交换机，然后由交换机发送给两个队列，两个消费者各自监听一个队列，来消费消息。
     * <p>
     * 这种方式实现同一个消息被多个消费者消费。工作模式是同一个消息只能有一个消费者。
     */
    @GetMapping("hello3")
    public void send3() {
        String context = "hi,fanout msg";
        System.out.println("Sender:" + context);
        template.convertAndSend("fanoutExchange", "", context);
    }

    /**
     * 发送端不只按固定的routing key发送消息，而是按字符串匹配发送，接收端同样如此
     * <p>
     * 符号#匹配一个或多个词，符号*匹配不多不少一个词。
     */

    @GetMapping("1")//只有topic.B能接收到
    public void send4() {
        String context = "hi, i am message all";
        System.out.println("Sender : " + context);
        template.convertAndSend("topicExchange", "topic.1", context);
    }

    @GetMapping("2")//topic.A和topic.B都能接收到
    public void send5() {
        String context = "hi, i am message 1";
        System.out.println("Sender : " + context);
        template.convertAndSend("topicExchange", "topic.message", context);
    }

    @GetMapping("3")//只有topic.B能接收到
    public void send6() {
        String context = "hi, i am messages 2";
        System.out.println("Sender : " + context);
        template.convertAndSend("topicExchange", "topic.messages", context);
    }
}
